package cn.java79.springboot.zsb.ctrl;

import cn.java79.springboot.zsb.config.OssConfig;
import cn.java79.springboot.zsb.pojo.AjaxResult;
import cn.java79.springboot.zsb.util.Md5Util;
import cn.java79.springboot.zsb.util.StrUtil;
import cn.java79.springboot.zsb.util.TentcentOssUtil;
import com.baomidou.mybatisplus.core.toolkit.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("common")
public class CommonController {
    @Autowired
    private OssConfig config;

    @PostMapping("upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        String houzhui = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
        String path = TentcentOssUtil.uploadFile2(file.getInputStream(), UUID.randomUUID() + houzhui, "/zsb");
        return AjaxResult.success(path);
    }

    //    @GetMapping("gencode")
//    public void vcode(HttpServletResponse resp) throws ServletException, IOException {
//
//        BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
//        Graphics g = bi.getGraphics();
//        Color c = new Color(200,155,255);
//        g.setColor(c);
//        g.fillRect(0, 0, 68, 22);
//        char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
//        Random r = new Random();
//        int len = ch.length,index;
//        StringBuffer sb = new StringBuffer();
//        for(int i=0;i<4;i++) {
//            index = r.nextInt(len);
//            g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));
//            g.drawString(ch[index]+"", (i*15)+3, 18);
//            sb.append(ch[index]);
//        }
//        String s = Md5Util.SHA256(sb.toString());
//        Cookie piccode = new Cookie("piccode", s);
//        piccode.setHttpOnly(true);//cookie js拿不到
//        piccode.setMaxAge(60*2);
//        resp.addCookie(piccode);
//
//        //request.getSession().setAttribute("piccode", sb.toString()+" "+System.currentTimeMillis());
//        ImageIO.write(bi, "JPG", resp.getOutputStream());
//    }
    @Autowired
    @Qualifier("stringRedisTemplate")
    private RedisTemplate template;
    @GetMapping("gencode")
    public AjaxResult vcode() throws ServletException, IOException {

        BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        Color c = new Color(200, 155, 255);
        g.setColor(c);
        g.fillRect(0, 0, 68, 22);
        char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
        Random r = new Random();
        int len = ch.length, index;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            index = r.nextInt(len);
            g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt(255)));
            g.drawString(ch[index] + "", (i * 15) + 3, 18);
            sb.append(ch[index]);
        }

        FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
        //request.getSession().setAttribute("piccode", sb.toString()+" "+System.currentTimeMillis());
        ImageIO.write(bi, "JPG", bout);
//        byte[] bytes = bout.toByteArray();
//        String s = StrUtil.toBase64(bytes);
        Map<String,String > map=new HashMap<>();
        String uid=UUID.randomUUID().toString();
        map.put("uid",uid);
        map.put("picbase64",StrUtil.toBase64(bout.toByteArray()));
        this.template.opsForValue().set("vcode::"+uid,sb.toString(), Duration.ofSeconds(500));
        return AjaxResult.success(map);
    }
}
